#include "qhistorydatabase.h"

#include "qsqlitecontrol.h"

#include "../qdata.h"

#include <QApplication>
#include <QStringList>

QHistoryDataBase::QHistoryDataBase():
    m_sqlite_control(new QSqliteControl)
{
}

QHistoryDataBase::~QHistoryDataBase()
{
    delete m_sqlite_control;
}

void QHistoryDataBase::open(const QString &path)
{
    m_sqlite_control->open(path+"/history_data.db");
    m_tables=m_sqlite_control->get_tables();
    if(!m_tables.contains("property"))
    {
        m_sqlite_control->create_table("property",QStringList()<<"name varchar(32) primary key"<<"value varchar(48)");
        m_tables=m_sqlite_control->get_tables();
    }
}

void QHistoryDataBase::set_property(const QString &key, const QString &value)
{
    m_sqlite_control->replace("property",QStringList()<<key<<value);
}

void QHistoryDataBase::close()
{
    m_sqlite_control->close();
}

void QHistoryDataBase::save_data(tagDataInfo *info, const QString &time)
{
    QString str=info->m_uuid;
    str.remove("{");
    str.remove("}");
    str.remove("-");
    if(!m_tables.contains(str))
    {
        m_sqlite_control->create_table("["+str+"]",QStringList()<<"time varchar(48)"<<"value varchar(32)");
        m_sqlite_control->insert("["+str+"]",QStringList()<<info->m_name<<info->m_data->get_name());
        m_tables=m_sqlite_control->get_tables();
    }
    m_sqlite_control->insert("["+str+"]",QStringList()<<time<<info->m_value.toString());
}
